Skip to content

Comments

[yulrang] 25.01.19#31

Merged
JooKangsan merged 23 commits intomainfrom
LeeYoul
Jan 23, 2025
Merged

[yulrang] 25.01.19#31
JooKangsan merged 23 commits intomainfrom
LeeYoul

Conversation

@yulrang
Copy link
Collaborator

@yulrang yulrang commented Jan 19, 2025

📌 푼 문제

순서쌍의 개수 | https://school.programmers.co.kr/learn/courses/30/lessons/120836
점의 위치 구하기 | https://school.programmers.co.kr/learn/courses/30/lessons/120841
로그인 성공? | https://school.programmers.co.kr/learn/courses/30/lessons/120883
특이한 정렬 | https://school.programmers.co.kr/learn/courses/30/lessons/120880

카드 뭉치 | https://school.programmers.co.kr/learn/courses/30/lessons/159994
공원 산책 | https://school.programmers.co.kr/learn/courses/30/lessons/172928
햄버거 만들기 | https://school.programmers.co.kr/learn/courses/30/lessons/133502

📝 간단한 풀이 과정

카드 뭉치

  • 간단한 문제접근법/풀이 설명
    카드 더미의 첫번째 값과 목표값 전체를 비교합니다.
    일치할 경우 카드 더미의 첫번째 값을 꺼내고, 일치하는 카드가 두 더미 모두에 없을 경우 종료합니다.
function solution(cards1, cards2, goal) {
  var answer = '';
  
  for(let i=0; i<goal.length; i++) {
      if(cards1[0] === goal[i] || cards2[0] === goal[i]){
          if(cards1[0] === goal[i]){
              cards1.shift();
          }
          if(cards2[0] === goal[i]){
              cards2.shift();
          }
          answer = "Yes";
      } else {
          answer = "No";
          break;
      }
  }
  
  return answer;
}

공원 산책

  • 간단한 문제접근법/풀이 설명

pathArr는 걸어간 좌표를 저장하는 배열입니다.
첫번째 반복문에서는 S를 찾아 pathArr의 첫번째 요소로 집어넣습니다.
두번째 반복문에서는 루트에 따라 걸음을 수행합니다.

걸어가는 방향에 따라 제일 첫번째 마주칠 수 있는 블럭의 포지션을 찾아 저장하고,
걸음을 수행하려했을 때 블럭과 충돌한다면 다음 루트로 넘어갑니다.

문제가 없을 경우 pathArr에 좌표를 저장하고 걸음을 수행합니다.

function solution(park, routes) {
  let pathArr = [];
  const WIDTH = park[0].length;
  const HEIGHT = park.length;
  
  for(let rowIdx = 0; rowIdx < park.length; rowIdx++){
      const row = park[rowIdx].split("");
      if(row.includes("S")){
          const columnIdx = park[rowIdx].indexOf("S");
          pathArr.push([columnIdx, rowIdx]);
          break;
      }
  }
  
  
  for(let i = 0; i<routes.length; i++) {
      const DIRECTION = routes[i].split(' ')[0];
      const AMOUNT = Number(routes[i].split(' ')[1]);
      let blockPos = [-1, -1];
      let x = pathArr[pathArr.length-1][0];
      let y = pathArr[pathArr.length-1][1];
      
      if (DIRECTION === "E") {
          blockPos = [park[y].indexOf("X", x), y];
          x = pathArr[pathArr.length-1][0] + AMOUNT;
          y = pathArr[pathArr.length-1][1];
      } else if (DIRECTION === "W"){
          blockPos = [park[y].lastIndexOf("X", x), y];
          x = pathArr[pathArr.length-1][0] - AMOUNT;
          y = pathArr[pathArr.length-1][1];
      } else if (DIRECTION === "S"){
          blockPos = [x, park.findIndex((row, idx) => idx >= y && row[x] === "X")];
          x = pathArr[pathArr.length-1][0];
          y = pathArr[pathArr.length-1][1] + AMOUNT;
      } else if (DIRECTION === "N"){
          blockPos = [x, park.slice(0, y).reverse().findIndex((row, idx) => row[x] === "X")];
          if (blockPos[1] !== -1) blockPos[1] = y - blockPos[1] - 1;
          x = pathArr[pathArr.length-1][0];
          y = pathArr[pathArr.length-1][1] - AMOUNT;
      }
  
      if (x < 0 || x >= WIDTH || y < 0 || y >= HEIGHT){
          continue;
      }
      if(!blockPos.includes(-1)){
          if((y === blockPos[1] && x >= blockPos[0] && pathArr[pathArr.length > 1 ? pathArr.length-2 : 0][0] < blockPos[0]) || 
             (y === blockPos[1] && x <= blockPos[0] && pathArr[pathArr.length > 1 ? pathArr.length-2 : 0][0] > blockPos[0]) ||
             (x === blockPos[0] && y >= blockPos[1] && pathArr[pathArr.length > 1 ? pathArr.length-2 : 0][1] < blockPos[1]) ||
              (x === blockPos[0] && y <= blockPos[1] && pathArr[pathArr.length > 1 ? pathArr.length-2 : 0][1] > blockPos[1]) ) {
              continue;
          }
      }
      
      pathArr.push([x, y]);
  }
  
  return pathArr[pathArr.length-1].reverse();
}


햄버거 만들기

  • 간단한 문제접근법/풀이 설명
    재료배열이 [1,2,3,1]의 배열을 가질 때 해당 배열을 삭제하고, 만든 햄버거의 개수를 증가시킵니다.
    4개의 요소를 탐색했으므로 인덱스는 4만큼만 삭제시키고 다시 검색합니다.
function solution(ingredient) {
  var answer = 0;
  
  for(let i=0; i<ingredient.length; i++) {
      if(ingredient[i] === 1 && ingredient[i+1] === 2 && ingredient[i+2] === 3 && ingredient[i+3] === 1) {
          ingredient.splice(i, 4);
          answer++;
          i -= 4;
      }
  }
  return answer;
}

@yulrang yulrang self-assigned this Jan 19, 2025
Copy link
Collaborator

@JooKangsan JooKangsan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

수고하셨어요!! 다음주도 화아팅입니다!

@JooKangsan JooKangsan merged commit cf0bad6 into main Jan 23, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants